<?php

namespace app\admin\controller\bushen;

use app\admin\controller\buchang\Exception;
use app\common\controller\Backend;
use think\Config;
use think\Db;
use think\exception\DbException;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\Loader;
use think\response\Json;
use think\Session;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Jiean extends Backend
{
    protected $noNeedLogin = ['*'];
    /**
     * Baoan模型对象
     * @var \app\admin\model\buchang\Baoan
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\buchang\Baoan;
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->site = Config::get('site');
        $this->view->assign('chexing', $this->site['chexing']);
        $this->view->assign('xingzhi', $this->site['xingzhi']);
        $this->view->assign('task', $this->site['task_list']);
        $this->view->assign('site', $this->site);
    }


    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }

        [$where, $sort, $order, $offset, $limit] = $this->buildparams();

        $wheres = '1=1';
        $list   = Db::name("baoan")
            ->alias('baoan')
            ->where($where)
            ->where($wheres)
            ->order('baoan.id desc')
            ->paginate($limit);


        $admin = $this->auth->getUserInfo();
        foreach ($list as $k => &$v) {
            if ($v['status'] == 5) {
                $v['task_1_count'] = "1/" . $v['task_1'];
            } else {
                $v['task_1_count'] = "0/" . $v['task_1'];
            }
            $v['task_2_count'] = Db::name("scwusun")->where("bid", $v['id'])->where("status", "5")->count() . '/' . $v['task_2'];
            $v['task_3_count'] = Db::name("sanche")->where("bid", $v['id'])->where("status", "5")->count() . '/' . $v['task_3'];
            $v['task_4_count'] = Db::name("benren")->where("bid", $v['id'])->where("status", "5")->count() . '/' . $v['task_4'];
            $v['task_5_count'] = Db::name("sanren")->where("bid", $v['id'])->where("status", "5")->count() . '/' . $v['task_5'];

            // 是否显示结案按钮
            if ($v['task_1'] != $v['task_1_count'] ||
                $v['task_2'] != $v['task_2_count'] ||
                $v['task_3'] != $v['task_3_count'] ||
                $v['task_4'] != $v['task_4_count'] ||
                $v['task_5'] != $v['task_5_count']
            ) {
                $v['jiean'] = 0;
            } else {
                $v['jiean'] = 1;
            }
            $list[$k] = $v;
        }


        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    /**
     * 审核
     * @param $type
     * @param $id
     * @return mixed
     * @throws DbException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function shenhe($type = null, $id = null)
    {
        $this->assign('orderid', $id);
        $hd_num   = 0;
        $hd_money = 0;
        $hd_sum   = 0;
        $sum       = 0;
        $ids      = $id;
        //本车信息************************************************************************************************************
        $baoan                      = Db::name('baoan')->where(['id' => $id])->find();
        $baoan['yw_class_text']     = Db::name('zuzhi')->where('id', $baoan['yw_class'])->value('name');
        $baoan['chakan_group_name'] = Db::name('zuzhi')->where('id', $baoan['chakan_group'])->value('name');
        if($baoan['task_1'] <> 0){
            $lisuan = Db::name('lisuan')->where('bid', $id)->where('sid', $id)->where('type',1)->find();
            $this->assign('lisuan', $lisuan);

            $shoukuan = Db::name('shoukuan')->where('bid', $id)->where('sid', $id)->select();
            $this->assign('shoukuan', $shoukuan);

            $qita = Db::name('qita')->where('bid', $id)->where('sid', $id)->select();
            $this->assign('qita', $qita);

            $peijian  = Db::name('peijian')->where('bid', $id)->select();
            $this->assign('peijian', $peijian);

            $bengong = Db::name('bengong')->where('bid', $id)->select();
            $bengong1 = 1;
            foreach ($bengong as $k => $v) {
                if ($v['num_he'] <> 0) {
                    $bengong1 = 2;
                }
            }
            $this->assign('bengong1', $bengong1);
            $this->assign('bengong', $bengong);
        }


        //三者车损************************************************************************************************************
        $ids2     = Db::name("sanche")->where("bid", $id)->value("id");
        $sanche = Db::name('sanche')->where('bid', $id)->select();

        if($ids2){
            $lisuan2 = Db::name('lisuan')->where('bid', $id)->where('sid', $ids2)->where('type',2)->find();
            $this->assign('lisuan2', $lisuan2);

            $shoukuan2 = Db::name('shoukuan')->where('bid', $id)->where('sid', $ids2)->select();
            $this->assign('shoukuan2', $shoukuan2);

            $qita2 = Db::name('qita')->where('bid', $id)->where('sid', $ids2)->select();
            $this->assign('qita2', $qita2);
        }
        $this->assign('sanche', $sanche);


        //三者财务损失***********************************************************************************************************
        $ids3      = Db::name("scwusun")->where("bid", $id)->value("id");
        $scwusun = Db::name('scwusun')->where('bid', $id)->select();

        if($ids3){
            $lisuan3 = Db::name('lisuan')->where('bid', $id)->where('sid', $ids3)->where('type',3)->find();
            $this->assign('lisuan3', $lisuan3);

            $shoukuan3 = Db::name('shoukuan')->where('bid', $id)->where('sid', $ids3)->select();
            $this->assign('shoukuan3', $shoukuan3);

            $qita3 = Db::name('qita')->where('bid', $id)->where('sid', $ids3)->select();
            $this->assign('qita3', $qita3);
        }
        foreach ($scwusun as $key => &$value) {
            $value['dong_hd_num']   = Db::name("sancai")->where("sid", $value['id'])->sum("num_he");
            $value['dong_hd_money'] = Db::name("sancai")->where("sid", $value['id'])->sum("price_he");
            $value['dong_hd_sum']   = Db::name("sancai")->where("sid", $value['id'])->sum("hejia");
            $sancai                 = Db::name('sancai')->where('sid', $value['id'])->field('sum(hejia) as he_ding,sum(money) as jin_e')->find();
            $value['he_ding']       = $sancai['he_ding'];
            $value['jin_e']         = $sancai['jin_e'];
        }
        $this->assign('scwusun', $scwusun);

        //人车人伤************************************************************************************************************
        $ids4     = Db::name("benren")->where("bid", $id)->value("id");
        $benren = Db::name('benren')->where('bid', $id)->select();

        if($ids4){
            $lisuan4 = Db::name('lisuan')->where('bid', $id)->where('sid', $ids4)->where('type',4)->find();
            $this->assign('lisuan4', $lisuan4);

            $shoukuan4 = Db::name('shoukuan')->where('bid', $id)->where('sid', $ids4)->select();
            $this->assign('shoukuan4', $shoukuan4);

            $qita4 = Db::name('qita')->where('bid', $id)->where('sid', $ids4)->select();
            $this->assign('qita4', $qita4);
        }
        $this->assign('benren', $benren);
        //三者人伤************************************************************************************************************
        $ids5     = Db::name("sanren")->where("bid", $id)->value("id");
        $sanren = Db::name('sanren')->where('bid', $id)->select();

        if($ids5){
            $lisuan5 = Db::name('lisuan')->where('bid', $id)->where('sid', $ids5)->where('type',5)->find();
            $this->assign('lisuan5', $lisuan5);

            $shoukuan5 = Db::name('shoukuan')->where('bid', $id)->where('sid', $ids5)->select();
            $this->assign('shoukuan5', $shoukuan5);

            $qita5 = Db::name('qita')->where('bid', $id)->where('sid', $ids5)->select();
            $this->assign('qita5', $qita5);
        }
        $this->assign('sanren', $sanren);
        //结束**************************************************************************************************************

        if ($type == 1) {

        } elseif ($type == 2) {
            $hd_num   = 0;
            $hd_money = 0;
            $hd_sum   = 0;
        } elseif ($type == 3) {
            $hd_num   = Db::name("sancai")->where("sid", $ids)->sum("num_he");
            $hd_money = Db::name("sancai")->where("sid", $ids)->sum("price_he");
            $hd_sum   = Db::name("sancai")->where("sid", $ids)->sum("hejia");
        } elseif ($type == 4) {


            $hd_sum = Db::name("renshang")->where("bid", $ids)->sum("hejia");
        } elseif ($type == 5) {
            $data_s_list = Db::name('renshang')->where('sid', $ids)->select();
            $sum       = 0;
            $hd_sum    = 0;
            foreach ($data_s_list as $value) {
                $sum    += floatval($value['price']) * floatval($value['num']);
                $hd_sum += floatval($value['hd_price']) * floatval($value['hd_num']);
            }
            $this->assign('sum', $sum);
            $this->assign('hd_sum', $hd_sum);
        }
        //查询服务单
        $list_one                  = Db::name('list_one')->where(['tong_number' => $baoan['tong_number']])->find();
        $list_one['yw_class_text'] = $baoan['yw_class_text'];
        $this->assign('row', $baoan);
        $this->assign('res', $list_one);
        //图片
        $file = Db::name('files')->where('bid', $id)->column('url');
        $this->assign('files', implode(',', $file));


        $this->assign('hd_num', $hd_num);
        $this->assign('hd_money', $hd_money);
        $this->assign('hd_sum', $hd_sum);
        $this->assign('sum', $sum);



//        $bencai = Db::name('bencai')->where('bid', $id)->select();
//        $this->assign('bencai', $bencai);



        $yijian = Db::name('byijian')->where('bid', $id)->order('id desc')->select();
        $this->assign('yijian', $yijian);

        $this->assign('bid', $baoan['id']);
        $this->assign('type', $type);
        return $this->fetch();
    }

    /**
     * @param $ids
     * @return void
     */
    public function jiean($ids)
    {
        // 启动事务
        Db::startTrans();
        try {
            Db::name('baoan')->where('id', $ids)->update(['baoan_status'=>1]);
            // 提交事务
            Db::commit();
            return json(['code' => 1, 'msg' => '结案成功']);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['code' => 0, 'msg' => '失败' . $e->getMessage().'，异常行：'.$e->getLine().'，异常路径：'.$e->getfile()]);
        }
    }



    /**
     * 添加补偿单意见
     * 合并成一个方法
     */
    private function add_baoyijian($bid, $tuihui, $type = 0)
    {
        $yijian = [
            'bid'     => $bid,
            'type'    => $type,
            'name'    => session::get('admin')['username'],
            'tuihui'  => $tuihui,
            'time'    => time(),
            'user_id' => session::get('admin')['id']
        ];
        Db::name('byijian')->insert($yijian);
    }


}
